﻿/// <reference path="../js/jquery.js" />
$.fn.DicTree = function (TypeCode, title, selectEvent) {
  
    function GetID() {
        return Number(Math.random().toString().substr(3, length) + Date.now()).toString(36);
    }
    function DictionaryTree() {
        var dicTreeObj = this;
     
        this.zTree = null;
        this.rMenu = null;
      
    }
    DictionaryTree.prototype.Init = function (TypeCode, $this, selectEvent) {
        this.Control = $this;
        this.TypeCode = TypeCode;
        var dicTreeObj = this;
        this.SelectEvent = selectEvent;
        this.setting = {
            view: {
                dblClickExpand: false
            },
            callback: {
                onRightClick: dicTreeObj.OnRightClick,
                onClick: dicTreeObj.zTreeOnClick
            }
        };
        AJAXAPI('/api/Dictionary/GetTreeData', { TypeCode: TypeCode }, function (d) {
            if (d.success) {
                var treeObj = $.fn.zTree.init($this.find(".ztree"), dicTreeObj.setting, d.data);
                dicTreeObj.zTree = $.fn.zTree.getZTreeObj($this.treeID);
                var nodes = treeObj.getNodes();
                if (nodes.length > 0) {
                    var node = treeObj.selectNode(nodes[0]);
                    dicTreeObj.zTree.selectNode(node);//选择点
                    dicTreeObj.zTree.setting.callback.onClick(null, dicTreeObj.zTree.setting.treeId, node);//调用事件
                }
                treeObj.expandAll(true);
                dicTreeObj.rMenu = $("#" + $this.treeID + "_rMenu");
                dicTreeObj.rMenu.find(".Add").click(function () {
                    dicTreeObj.add();
                })
                dicTreeObj.rMenu.find(".Edit").click(function () {
                    dicTreeObj.edit();
                });
                dicTreeObj.rMenu.find(".Delete").click(function () {
                    dicTreeObj.Del();
                });
            } else {
                ShowError(d.msg);
            }
        });
    }
    ///
    DictionaryTree.prototype.OnRightClick = function (event, treeId, treeNode) {
        if (treeNode) {
            dictionaryTreeObj[treeId].zTree.selectNode(treeNode, false, true);

            var offset = $("#" + treeNode.tId + " a").offset();
            dictionaryTreeObj[treeId].ShowRMenu(treeNode.typeid, (offset.left + $("#" + treeNode.tId + " a").outerWidth(true)), offset.top);

        }
    }
    DictionaryTree.prototype.zTreeOnClick = function (event, treeId, treeNode) {
        if (treeId != undefined && treeId!="") {
            if (typeof (dictionaryTreeObj[treeId].SelectEvent) == "function") {
                dictionaryTreeObj[treeId].SelectEvent(event, treeId, treeNode);
            }
        }
    }
    DictionaryTree.prototype.ShowRMenu = function (typeid, x, y) {
        if (x > 218) x = 218;
        this.rMenu.css({ "top": y + "px", "left": x + "px", "z-index": "100000" }).show();
        var $this = this;
        $("body").mousedown(function (event) {
            $this.onBodyMouseDown(event);
        })
      
    }
    //隐藏面板
    DictionaryTree.prototype.hideRMenu = function () {
        if (this.rMenu) this.rMenu.hide();
        $("body").unbind("mousedown");
    }
    //点击非按钮区域消失
    DictionaryTree.prototype.onBodyMouseDown = function (event) {
        if (!(event.target.id == this.rMenu.attr("id") || $(event.target).parents("#" + this.rMenu.attr("id")).length > 0)) {
            this.rMenu.hide();
        }
    }
    DictionaryTree.prototype.View = function (fid, fparentId, IsView) {
        var $this = this;
        var nodes = $this.zTree.getSelectedNodes();
        AJAX('/Content/Resources/html/DictionaryInfo.html?v=11', {}, function (d) {
            var html = d;
            parent.layer.open({
                type: 1,
                title: '<span>' + title + '</span>',
                content: html,
                anim: 'up',
                success: function (layero, index) {
                    Dictionarydialog = $(layero);
                    parent.dialogResize(Dictionarydialog, 800, 400);
                    if (typeof (fid) != "undefined" && fid!=null) {
                        AJAXAPI('/api/Dictionary/GetData', fid, function (d) {
                            if (d.success) {
                                var data = d.data;
                                Dictionarydialog.find("#txtFName").val(data.FName);
                                Dictionarydialog.find("#txtFCode").val(data.FCode);
                                Dictionarydialog.find("#txtFCode").attr("disabled", "disabled");
                                Dictionarydialog.find("#txtFRemark").val(data.FRemark);
                                Dictionarydialog.find("#hidFParentID").val(data.FParentID);
                            } else {
                                ShowError(d.msg);
                                return false;
                            }
                        });
                    }
                    else {
                        if (nodes[0].typeid == 1) {
                            Dictionarydialog.find("#hidFParentID").val(nodes[0].fid);
                        } else {
                            Dictionarydialog.find("#hidFParentID").val("");
                        }
                    }
                    if (IsView) {
                        Dictionarydialog.find("#txtFName").attr("disabled", "disabled");
                        Dictionarydialog.find("#txtFCode").attr("disabled", "disabled");
                        Dictionarydialog.find("#txtFRemark").attr("disabled", "disabled");
                    }
                    ///关闭方法
                    Dictionarydialog.find("#butclose").click(function () {
                        parent.layer.close(Dictionarydialog.attr("Index"));
                    });
                    ///关闭方法
                    Dictionarydialog.find("#butSave").click(function () {
                        var param = {
                            FName: Dictionarydialog.find("#txtFName").val(),
                            FCode: Dictionarydialog.find("#txtFCode").val(),
                            FRemark: Dictionarydialog.find("#txtFRemark").val(),
                            FID: fid,
                            FTypeCode: $this.TypeCode,
                            FParentID: Dictionarydialog.find("#hidFParentID").val()
                        }
                        if (param.FName == "") {
                            ShowError("字典名称不能为空！");
                            return;
                        }
                        if (param.FCode == "") {
                            ShowError("字典编码不能为空！");
                            return;
                        }
                        AJAXAPI('/api/Dictionary/Save', param, function (d) {
                            if (d.success) {
                                ShowError("保存成功！");
                                if (fid) {
                                    nodes[0].name = param.FName;
                                    nodes[0].index = 1;
                                    nodes[0].code = param.FCode;
                                    $this.zTree.updateNode(nodes[0]);
                                  
                                } else {
                                    var newNode = {
                                        fid: d.data.FID,
                                        name: param.FName,
                                        typeid: 1,
                                        code: param.FCode,
                                        procbid: param.FParentID,
                                        open: true
                                    };
                                    $this.zTree.addNodes(nodes[0], newNode);
                                }
                                parent.layer.close(Dictionarydialog.attr("Index"));
                            } else {
                                ShowError(d.msg);
                                return false;
                            }
                        });

                    });
                }
            });
        }, "GET");
    }
    DictionaryTree.prototype.Del = function (fid) {
        this.rMenu.hide();
        var nodes = this.zTree.getSelectedNodes();
        if (nodes && nodes.length > 0) {
            var cur_node = nodes[0];
            if (cur_node.children && cur_node.children.length > 0) {
                ShowError('请先删除节点下的子节点');
                return;
            }
            Confirm('确定删除吗？', function (index) {
                AJAXAPI('/api/Dictionary/Del', nodes[0].fid, function (d) {
                    if (d.success) {
                        ShowError(d.msg);
                        this.zTree.removeNode(cur_node);

                    } else {
                        ShowError(d.msg);
                        return false;
                    }
                });
            });
        }

    }
    DictionaryTree.prototype.edit = function () {
        this.rMenu.hide();
        var nodes = this.zTree.getSelectedNodes();
        if (nodes && nodes.length > 0) {
            this.View(nodes[0].fid, nodes[0].procbid);
        }
    }
    DictionaryTree.prototype.add = function () {
        this.rMenu.hide();
        var nodes = this.zTree.getSelectedNodes();
        if (nodes && nodes.length > 0) {
            this.View(null, nodes[0].fid);
        }
    }
    var dictionaryTreeObj = {};
    this.each(function () {
        var $this = $(this);
        if ($this.find(".ztree").length <= 0) {
            $this.treeID = 'treeDemo_' + GetID();
            $this.append('<ul id="' + $this.treeID + '" class="ztree"></ul> <div class="clear"></div>');
            $(document.body).append('<div id="' + $this.treeID + '_rMenu" class="rMenu" style="display:none"><ul> <li id="m_add_one" class="Add"><i class="fa fa-plus"></i> 新增</li> <li  id="m_edit" class="Edit"><i class="fa fa-edit"></i> 修改</li>        <li  id="m_del" class="Delete"><i class="fa fa-remove"></i>&nbsp;&nbsp;删除</li>    </ul></div>');
            dictionaryTreeObj[$this.treeID] = new DictionaryTree();
            dictionaryTreeObj[$this.treeID].Init(TypeCode, $this,selectEvent);
        }
    });
}